<?php
namespace cms\modules\backend\controllers;

use Yii;
use yii\db\Expression;
use cms\controllers\AuthController;
use app\models\Message;
use app\models\MessageSearch;
use common\component\exception\ErrMap;
use common\component\tool\Tool;
use common\models\OperationLog;


class MessageController extends AuthController
{
    /*列表*/
    public function actionMessageList()
    {
        if(isset($this->get['page'])){
            $currPage = intval($this->get['page']?$this->get['page']:1);
        }else{
            $currPage = 1;
        }
        $pageSize = Tool::getPageSize();
        
        $this->get['app_id'] = $this->app_id;
        $search = (new MessageSearch())->search($this->get);
        if($search === false){//验证搜索参数不通过
            $count = 0;
            $messageList = array();
            $basicConfig = [];
        }else{
            $count = $search->count();
            $messageList = $search->select(['mid', 'title', 'name', 'companyname', 'phone', 'contents', 'add_time', 'reply', 'replyname', 'reply_time'])
                          ->offset($pageSize*($currPage-1))->limit($pageSize)
                          ->orderBy(['mid' => SORT_DESC])
                          ->asArray()->all();

        }
        $res = array(
            'list' => $messageList,
            'pageInfo' => array(
                'count' => intval($count),
                'currPage' => $currPage,
                'pageSize' => $pageSize,
            ),
        );
        return Tool::showRes([0, '获取成功'], $res);
    }

    /*删除*/
    public function actionDeleteMessage()
    {
        $id = isset($this->get['id'])?$this->get['id']:'';
        if($id){
            $message = new Message;
            if($message->deleteAll('mid = :id', [':id' => $id])){//删除成功
                $backInfo = [0, '删除留言成功'];//返回信息
            }else{//删除失败
                $backInfo = ErrMap::getError(10013, '删除留言失败');//返回信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), '') ) != 'boolean'){
                return $res;
            };

        }
        return Tool::showRes(ErrMap::getError(10005));
    }

}
